package com.alcj.controller;

import com.alcj.common.constant.SysConstant;
import com.alcj.common.framework.PageController;
import com.alcj.common.utils.CmnUtil;
import com.alcj.model.ContactUs;
import com.alcj.model.FirmCulture;
import com.alcj.model.LawNous;
import com.alcj.model.News;
import com.alcj.service.CompanyAboutService;
import com.alcj.service.LawNousService;
import com.alcj.service.NewsService;
import com.alibaba.fastjson.JSONObject;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author :chenxh
 * @version :v1.0
 * @Package :com.alcj.controller
 * @Description :${TODO}(新闻资讯法规常识管理)
 * @date :
 */
@Controller
@RequestMapping("/NewsNous")
public class NewsNousController extends PageController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    NewsService newsService;
    @Autowired
    LawNousService lawNousService;
    /**
     * @Author: chenxh
     * @Description:
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/laws")
    public String getLawsList(HttpServletRequest request, Model model) {
        try {
            // 法律法规
            LawNous paramInfo = new LawNous();
            paramInfo.setLawnoustype("0");
            // 获取页数
            String currentPage = request.getParameter("currentPage");
            // 设置页数
            if (CmnUtil.isEmpty(currentPage)) {
                currentPage = "1";
            }
            super.setCurrentPage(Integer.parseInt(currentPage));
            // 设置总件数数
            int total = lawNousService.getPageLawsCount(paramInfo);
            super.giveTotalNum(total);
            if(total > 0){
                // 设置分页参数
                super.preparePageParams(paramInfo);
                // 设定检索条件
                List<LawNous> resultInfo = lawNousService.getPageLawInfo(paramInfo);
                model.addAttribute("newsList", resultInfo);
            }
            model.addAttribute("j_page", getPage());
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "lawnousList";
    }

    /**
     * @Author: chenxh
     * @Description:
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/lawsInfo")
    public String getLawsInfo(HttpServletRequest request, Model model) {
        try {
            String lawsId = (String) request.getParameter("lawsId");
            // 设定检索条件
            LawNous resultInfo = lawNousService.getDetailInfo(Integer.parseInt(lawsId));

            model.addAttribute("resultInfo", resultInfo);
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "lawnousDetail";
    }
    /**
     * @Author: chenxh
     * @Description:
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/news")
    public String getNewssList(HttpServletRequest request, Model model) {
        try {
            // 公司新闻
            News paramInfo = new News();
            paramInfo.setNewstype("1");
            // 获取页数
            String currentPage = request.getParameter("currentPage");
            // 设置页数
            if (CmnUtil.isEmpty(currentPage)) {
                currentPage = "1";
            }
            super.setCurrentPage(Integer.parseInt(currentPage));
            // 设置总件数数
            int total = newsService.getPageNewsCount(paramInfo);
            super.giveTotalNum(total);
            if(total > 0){
                // 设置分页参数
                super.preparePageParams(paramInfo);
                // 设定检索条件
                List<News> resultInfo = newsService.getPageNewsInfo(paramInfo);
                model.addAttribute("newsList", resultInfo);
            }
            model.addAttribute("j_page", getPage());
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "newsList";
    }

    /**
     * @Author: chenxh
     * @Description:
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/newsInfo")
    public String getNewsInfo(HttpServletRequest request, Model model) {
        try {
            String newsId = (String) request.getParameter("newsId");
            // 设定检索条件
            News resultInfo = newsService.getDetailInfo(Integer.parseInt(newsId));

            model.addAttribute("resultInfo", resultInfo);
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "newsDetail";
    }
    /**
     * @Author: chenxh
     * @Description: 行业资讯
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/newsIndustry")
    public String getNewsIndustryList(HttpServletRequest request, Model model) {
        try {
            // 行业资讯
            News paramInfo = new News();
            paramInfo.setNewstype("0");
            // 获取页数
            String currentPage = request.getParameter("currentPage");
            // 设置页数
            if (CmnUtil.isEmpty(currentPage)) {
                currentPage = "1";
            }
            super.setCurrentPage(Integer.parseInt(currentPage));
            // 设置总件数数
            int total = newsService.getPageNewsCount(paramInfo);
            super.giveTotalNum(total);
            if(total > 0){
                // 设置分页参数
                super.preparePageParams(paramInfo);
                // 设定检索条件
                List<News> resultInfo = newsService.getPageNewsInfo(paramInfo);
                model.addAttribute("newsList", resultInfo);
            }
            model.addAttribute("j_page", getPage());
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "newsIndustryList";
    }

    /**
     * @Author: chenxh
     * @Description:
     * @Date: 2017/9/7 16:13
     * @param: request
     * @param: model
     */
    @RequestMapping("/newsIndustryInfo")
    public String getNewsIndustry(HttpServletRequest request, Model model) {
        try {
            String newsId = (String) request.getParameter("newsId");
            // 设定检索条件
            News resultInfo = newsService.getDetailInfo(Integer.parseInt(newsId));

            model.addAttribute("resultInfo", resultInfo);
        } catch (Exception e) {
            // 异常报错
            logger.error("获取首页信息异常 : {}", e.getMessage());
            logger.error("异常详细信息 :",e);
            return SysConstant.ERROR_PAGE_500;
        }

        return "newsIndustryDetail";
    }
}
